package com.newtouch.bxzs.database.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.newtouch.bxzs.common.base.response.BaseResponse;
import com.newtouch.bxzs.common.base.response.RespGenerator;
import com.newtouch.bxzs.database.config.MyLog;
import com.newtouch.bxzs.database.model.bo.RoleBO;
import com.newtouch.bxzs.database.model.bo.RoleDetailBO;
import com.newtouch.bxzs.database.model.vo.GetAllRoleVO;
import com.newtouch.bxzs.database.model.vo.GetRoleListVO;
import com.newtouch.bxzs.database.model.vo.UpdateRoleVO;
import com.newtouch.bxzs.database.model.vo.UpdateStateVO;
import com.newtouch.bxzs.database.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色Controller:
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/4 14:21</td>
 * <td>zhangqianyang</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author zhangqianyang
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequestMapping(value = "/role")
@Api(tags = "后台维护角色信息")
@SuppressWarnings("unchecked")
public class RoleController {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Resource
    private RoleService roleService;

    @PostMapping("/updateRole")
    @ApiOperation("添加/修改角色")
    @MyLog(systemFunctionName = "后台维护角色信息", systemFunctionUrl = "/role/updateRole", userOperationType = 5, userOperationSummary = "添加/修改角色")
    public BaseResponse<Object> modifyRole(@RequestBody UpdateRoleVO updateRoleVO) {
        logger.info("添加/修改角色");
        roleService.updateRole(updateRoleVO);
        return RespGenerator.returnOK("操作成功");

    }

    @PostMapping("/getRoleList")
    @ApiOperation("查看角色列表")
    @MyLog(systemFunctionName = "后台维护角色信息", systemFunctionUrl = "/role/getRoleList", userOperationType = 4, userOperationSummary = "查看角色列表")
    public BaseResponse<IPage<RoleBO>> getRoleList(@RequestBody GetRoleListVO roleListVO) {
        logger.info("查看角色列表");
        IPage<RoleBO> result = roleService.listRole(roleListVO);
        // 如果 查询结果为空，返回"没有查询到相应数据"
        if (result == null || result.getTotal() == 0) {
            return RespGenerator.returnOK("没有查询到相应数据");
        }
        return RespGenerator.returnOK(result);
    }

    @PostMapping("/getAllRole")
    @ApiOperation("查看全部角色列表（不分页）")
    @MyLog(systemFunctionName = "后台维护角色信息", systemFunctionUrl = "/role/getAllRole", userOperationType = 4, userOperationSummary = "查看全部角色")
    public BaseResponse<List<RoleBO>> getAllRole(@RequestBody GetAllRoleVO getAllRoleVO) {
        logger.info("查看全部角色列表（不分页）");
        List<RoleBO> result = roleService.listAllRole(getAllRoleVO);
        // 如果 查询结果为空，返回"没有查询到相应数据"
        if (result == null || result.isEmpty()) {
            return RespGenerator.returnOK("没有查询到相应数据");
        }
        return RespGenerator.returnOK(result);
    }

    @GetMapping("/getRoleDetail")
    @ApiOperation("查看角色详情")
    @MyLog(systemFunctionName = "后台维护角色信息", systemFunctionUrl = "/role/getRoleDetail", userOperationType = 4, userOperationSummary = "查看角色详情")
    public BaseResponse<RoleDetailBO> getRoleDetail(@RequestParam @ApiParam(value = "角色id", required = true) String roleId) {
        logger.info("查看角色详情");
        RoleDetailBO result = roleService.getRoleDetail(roleId);
        return RespGenerator.returnOK(result);
    }

    @PostMapping("/updateState")
    @ApiOperation("启用/禁用角色")
    @MyLog(systemFunctionName = "后台维护角色信息", systemFunctionUrl = "/role/updateState", userOperationType = 2, userOperationSummary = "启用/禁用角色")
    public BaseResponse<String> updateState(@RequestBody UpdateStateVO updateStateVO) {
        logger.info("启用/禁用角色");
        roleService.updateState(updateStateVO);
        return RespGenerator.returnOK("操作成功");
    }

    @PostMapping("/deleteRole")
    @ApiOperation("删除角色")
    @MyLog(systemFunctionName = "后台维护角色信息", systemFunctionUrl = "/role/deleteRole", userOperationType = 3, userOperationSummary = "删除角色")
    public BaseResponse<String> deleteRole(@RequestBody @ApiParam(value = "角色id,数组，不用传参数名", required = true) List<String> roleIdList) {
        logger.info("删除角色");
        roleService.deleteRole(roleIdList);
        return RespGenerator.returnOK("操作成功");
    }
}
